﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;


public partial class xcha : System.Web.UI.Page
{
    private static string sqlconn = System.Configuration.ConfigurationManager.AppSettings["sqlcon"].ToString().Trim();
    private string sql = "select * from X_TABLE where 1=1 ";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Binddate();
            gvxiaoshou.Visible = false;
            Bindy();
           
        }
       
    }
    protected void Binddate()
    {
        users us = new users();
        gvxiaoshou.DataSource = us.GetAllxiaoshou(us);
        gvxiaoshou.DataBind();
    }
  
    void Bindy()
    {
        users us = new users();    //绑定
        drpyname.DataSource = us.Bindyao().Tables[0].DefaultView;
        drpyname.DataTextField = "Y_NAME";
        drpyname.DataValueField = "Y_ID";
        drpyname.DataBind();

    }
    protected void btncha0_Click(object sender, EventArgs e)
    {
        string dh = txthao.Text.Trim();
        string name = drpyname.SelectedValue.Trim();
       
        string starttime = txttime.Text.Trim();
        string endtime = txttimee.Text.Trim();
        SqlConnection coon = new SqlConnection(sqlconn);
        try
        {
            coon.Open();
            SqlCommand comm = new SqlCommand(sql, coon);
            SqlDataReader rd = comm.ExecuteReader();
            if (rd.Read())
            {
                coon.Close();
                coon.Open();
                //Response.Write("<script>alert(\"已查询到\")</script>");
                SqlDataAdapter adp = new SqlDataAdapter(sql, coon);
                try
                {
                    DataSet ds = new DataSet();
                    adp.Fill(ds, "X_TABLE");
                    //DataSet ds = new DataSet();
                    gvxiaoshou.DataSource = ds.Tables[0].DefaultView;
                    gvxiaoshou.DataBind();
                    gvxiaoshou.Visible = true;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    coon.Close();
                }
            }
            else
            {
                Response.Write("<script>alert(\"对不起，查询不到该条件下的信息！\")</script>");
            }
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            coon.Close();
        }
    }
    protected void btncin_Click(object sender, EventArgs e)
    {
        txthao.Text = null;
        txttime.Text = null;
        txttimee.Text = null;
    }
    protected void gvjinhuo_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvxiaoshou.PageIndex = e.NewPageIndex;
        Binddate();
    }
    protected void gvjinhuo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvxiaoshou.EditIndex = -1;
        Binddate();
    }
    protected void gvjinhuo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        users tmp = new users();
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowIndex == gvxiaoshou.EditIndex)
            {
                DataRowView rowItem = (DataRowView)e.Row.DataItem;


            }
        }
        try
        {
            //获取”删除“链接
            LinkButton InkbtnDelete = (LinkButton)e.Row.Cells[15].Controls[0];
            //添加JavaScript代码实现客户端信息的提示
            InkbtnDelete.OnClientClick = "return confirm('请确认" + e.Row.Cells[1].Text + "的记录删除吗？');";

        }
        catch
        {

        }
    }
    protected void gvjinhuo_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = gvxiaoshou.DataKeys[e.RowIndex].Value.ToString().Trim();
        users us = new users();
        us.id = id;
        us.Deletexiaoshou(us);
        Binddate();
    }
    protected void gvjinhuo_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvxiaoshou.EditIndex = e.NewEditIndex;
        Binddate();
    }
    protected void gvjinhuo_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = gvxiaoshou.DataKeys[e.RowIndex].Value.ToString().Trim();
        string time = ((TextBox)(gvxiaoshou.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim();
        string dh = ((TextBox)(gvxiaoshou.Rows[e.RowIndex].Cells[0].Controls[0])).Text.Trim();
        string name = ((TextBox)(gvxiaoshou.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim();
        string number = ((TextBox)(gvxiaoshou.Rows[e.RowIndex].Cells[4].Controls[0])).Text.Trim();
        string price = ((TextBox)(gvxiaoshou.Rows[e.RowIndex].Cells[5].Controls[0])).Text.Trim();
        string tname = ((TextBox)(gvxiaoshou.Rows[e.RowIndex].Cells[6].Controls[0])).Text.Trim();
        string ze = ((TextBox)(gvxiaoshou.Rows[e.RowIndex].Cells[7].Controls[0])).Text.Trim();
        users us = new users();
        us.dh = dh;
        us.time = time;
        us.id = id;
        us.name = name;
        us.number = number;
        us.price = price;
        us.ze = ze;
        us.tname = tname;

        us.Updaxiaoshou(us);
        Binddate();
    }
    protected void gvjinhuo_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void gvjinhuo_Sorting(object sender, GridViewSortEventArgs e)
    {
        users us = new users();
        string sortExpression = e.SortExpression;
        if (gvxiaoshou.SortDirection == SortDirection.Ascending)
        {
            DataView dv = us.GetAllxiaoshou(us).Tables[0].DefaultView;
            dv.Sort = sortExpression + "DESC";
            gvxiaoshou.DataSource = dv;
            gvxiaoshou.DataBind();
        }
        else
        {
            DataView dv = us.GetAllxiaoshou(us).Tables[0].DefaultView;
            dv.Sort = sortExpression + "ASC";
            gvxiaoshou.DataSource = dv;
            gvxiaoshou.DataBind();
        }
    }
    protected void ckyao_CheckedChanged(object sender, EventArgs e)
    {
        string dh = txthao.Text.Trim();
        string name = drpyname.SelectedValue.Trim();
     
        string starttime = txttime.Text.Trim();
        string endtime = txttimee.Text.Trim();

        if (chdh.Checked) sql += "and X_DANHAO ='" + dh + "'";
        if (chyname.Checked) sql += "and X_NAME ='" + name + "'";
       
        if (chqtime.Checked) sql += "and X_TIME >='" + starttime + "'";
        if (chjtime.Checked) sql += "and X_TIME <= '" + endtime + "'";
    }
}